home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-05 / lsshdown.zip / LSSHDOWN.C < prev    next >
C/C++ Source or Header  |  1992-12-18  |  5KB  |  206 lines

  1. #define INCL_32
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. /* OS/2 */
  7.  
  8. #define INCL_NOCOMMON
  9. #define INCL_DOSPROCESS
  10. #define INCL_DOSDATETIME
  11. #define INCL_DOSMISC
  12. #define INCL_DOSSEMAPHORES
  13. #define INCL_DOSFILEMGR
  14. #include <os2.h>
  15.  
  16. /* net */
  17.  
  18. #include "netcons.h"
  19. #include "service.h"
  20. #include "neterr.h"
  21. #include "use.h"
  22. #include "shares.h"
  23. #include "wksta.h"
  24.  
  25. static struct file_info_1 fileinfo[100];
  26. static struct use_info_0 useinfo[50];
  27. static struct session_info_2 sessinfo[50];
  28. static struct service_info_0 svcinfo0[50];
  29. static struct service_info_2 svcinfo;
  30.  
  31. static ULONG Reserved=0;
  32. static APIRET rc;
  33.  
  34. static USHORT nread,navail,nretry,needretry;
  35.  
  36. static HEV SemHandle;         /* semaphore handle for AsyncTimer */
  37.  
  38. static int i;
  39. static char slashstring[40];
  40.  
  41. static void wait100(void);
  42.  
  43. int main(void)
  44. {
  45.  
  46. /* in trap or hard error, stop without a pop-up window */
  47.  
  48. DosError(FERR_DISABLEHARDERR | FERR_DISABLEEXCEPTION);
  49.  
  50. /* close files: SERVER only */
  51.  
  52. rc=NetFileEnum(NULL,NULL,1,(char *)fileinfo,sizeof(fileinfo),&nread,&navail);
  53. if (rc==0) /* skip if wksta */
  54.  for (i=0; i<nread; i++)
  55.   {
  56.   /*
  57.   printf("Closing file %s for %s.\n",fileinfo[i].fi1_pathname,fileinfo[i].fi1_username);
  58.   */
  59.   rc=NetFileClose(NULL,fileinfo[i].fi1_id);
  60.   }
  61.  
  62. /* close uses: WKSTA as well */
  63.  
  64. rc=NetUseEnum(NULL,0,(char *)useinfo,sizeof(useinfo),&nread,&navail);
  65. if (rc==0)
  66.  for (i=0; i<nread; i++)
  67.   {
  68.   rc=NetUseDel(NULL,useinfo[i].ui0_local,USE_LOTS_OF_FORCE);
  69.   if (rc!=0) /* If error, try remote name: 3Com hack */
  70.    rc=NetUseDel(NULL,useinfo[i].ui0_remote,USE_LOTS_OF_FORCE);
  71. /* rc=2550 if, e.g., a remote drive is current for some process - OK */
  72.   }
  73.  
  74. /* It would be nice to logoff users to update DCDB properly
  75.    but there doesn't seem to be such an API! */
  76.  
  77. /*
  78. rc=NetLogonEnum(NULL,2,(char *)logoninfo,sizeof(logoninfo),&nread,&navail);
  79. if (rc==0)
  80.  for (i=0; i<nread; i++)
  81.   {
  82.   printf("Logging off %s on %s.\n",
  83.    logoninfo[i].usrlog2_eff_name,logoninfo[i].usrlog2_computer);
  84.   rc=Net???(NULL,logoninfo[i].usrlog2_eff_name);
  85.   }
  86. */
  87.  
  88. /* close sessions */
  89.  
  90. rc=NetSessionEnum(NULL,2,(char *)sessinfo,sizeof(sessinfo),&nread,&navail);
  91. if (rc==0)
  92.  for (i=0; i<nread; i++)
  93.   {
  94.   strcpy(slashstring,"\\\\");
  95.   strcat(slashstring,sessinfo[i].sesi2_cname);
  96. /*
  97.   printf("Deleting session %s (%s,%s).\n",slashstring,
  98.   sessinfo[i].sesi2_username,
  99.   sessinfo[i].sesi2_cltype_name);
  100. */
  101.   rc=NetSessionDel(NULL,slashstring,0);
  102.   }
  103.  
  104. /* close shares --- not really necessary
  105.  
  106. rc=NetShareEnum(NULL,2,(char *)sharinfo,sizeof(sharinfo),&nread,&navail);
  107. if (rc==0)
  108.  for (i=0; i<nread; i++)
  109.   {
  110.   printf("Deleting share %s / %s / %s",sharinfo[i].shi2_netname,
  111.    (sharinfo[i].shi2_type==STYPE_DISKTREE ? "disk" :(
  112.    sharinfo[i].shi2_type==STYPE_PRINTQ ? "printer" :(
  113.    sharinfo[i].shi2_type==STYPE_DEVICE ? "device" :(
  114.    sharinfo[i].shi2_type==STYPE_IPC ? "IPC" :
  115.                                "unknown type!"
  116.    )))),
  117.    sharinfo[i].shi2_remark,sharinfo[i].shi2_path);
  118.   if (*sharinfo[i].shi2_path)
  119.    printf(" / %s",sharinfo[i].shi2_path);
  120.   if (sharinfo[i].shi2_current_uses)
  121.    printf(" (%u user(s))",sharinfo[i].shi2_current_uses);
  122.   printf(".\n");
  123.   rc=NetShareDel(NULL,sharinfo[i].shi2_netname,0);
  124.   }
  125.  
  126. */
  127.  
  128. /* stop services: WKSTA and SERVER */
  129.  
  130. /* Create event semaphore used by wait100 */
  131. rc=DosCreateEventSem("\\SEM32\\SHDOASYNCTIMER",&SemHandle,0,0);
  132.  
  133. nretry=100;
  134. while(nretry)
  135.  {
  136.  needretry=0;
  137.  rc=NetServiceEnum(NULL,0,(char *)svcinfo0,sizeof(svcinfo0),&nread,&navail);
  138.  if (rc==0)
  139.   for (i=nread-1; i>=0; i--) /* top to bottom */
  140.    {
  141.    rc=NetServiceControl(NULL,svcinfo0[i].svci0_name, SERVICE_CTRL_UNINSTALL,
  142.     0, (char *)&svcinfo, sizeof(svcinfo) );
  143.  switch(rc)
  144.   {
  145.   case 0:
  146.    if ((svcinfo.svci2_status&SERVICE_INSTALL_STATE)!=SERVICE_UNINSTALLED)
  147.     {
  148.     needretry=1;
  149.     }
  150.   case NERR_ServiceEntryLocked:
  151.   case NERR_ServiceCtlBusy:
  152.   case NERR_ServiceTableLocked:
  153.   case NERR_ServiceCtlTimeout:
  154.    needretry=1;
  155.    break;
  156.   case NERR_ServiceNotCtrl:
  157.   case NERR_ServiceNotInstalled:
  158.   case NERR_ServiceKillProc:
  159.   case NERR_ServiceCtlNotValid:
  160.   default:
  161.    break;
  162.   }
  163.   }
  164.  if (needretry)
  165.   {
  166.   wait100();
  167.   nretry--;
  168.   }
  169.  else
  170.   nretry=0;
  171.  }
  172.  
  173. /* if we wanted to shut down in a more cute way,
  174.  
  175. #define INCL_WINWORKPLACE
  176.  
  177. if ((hab=WinInitialize(0))!=0)
  178.  {
  179.  if ((hmq=WinCreateMsgQueue(hab,0))!=0L)
  180.   {
  181.   rc=WinShutdownSystem(hab, hmq);
  182.   }
  183.  }
  184.  
  185. */
  186.  
  187.  
  188. /* from prog guide vol I p. 2-14: */
  189.  
  190. rc=DosShutdown(Reserved);
  191. DosBeep(1380,1000); /* indicate that shutdown has started */
  192. for(;;); /* freeze forever --- calling any function crashes the system */
  193.  
  194. }
  195.  
  196. void wait100(void)
  197. {
  198.  
  199. rc=DosResetEventSem(SemHandle,&rc);
  200.  
  201. rc=DosAsyncTimer(100,(HSEM)SemHandle,&rc);
  202.  
  203. rc=DosWaitEventSem(SemHandle,200);
  204.  
  205. }
  206.